package stattracker;

import java.util.ArrayList;

public class Playlist implements Comparable<Playlist>, java.io.Serializable
{
	private static final long serialVersionUID = 1L;
	private String title;
	private ArrayList<Halo3GameData> games;
	private int kills, deaths, assists, wins, losses, timePlayed;
	private double avgKill, avgDeath, avgAssist, sdKill, sdDeath, sdAssist;
	private boolean isRanked;
	
	public Playlist(String title, boolean ranked)
	{
		this.title = title;
		isRanked = ranked;
		games = new ArrayList<Halo3GameData>();
		resetNumbers();
	}
	
	public void compileStatistics()
	{
		resetNumbers();
		for(Halo3GameData g : games)
		{
			if(!g.getInfo().getGameType().equals("Broken Page"))
			{
				timePlayed += MyUtils.parseSec(g.getDetail().getLength());
				kills += g.getDetail().getKills();
				deaths += g.getDetail().getDeaths();
				assists += g.getDetail().getAssists();
				if(g.getInfo().getPlacement().equals("1st"))
					wins++;
				else
					losses++;
			}
		}
		
		avgKill = ((double)kills/games.size());
		avgDeath = ((double)deaths/games.size());
		avgAssist = ((double)assists/games.size());
		double varKills = 0, varDeaths = 0, varAssists = 0;
		for(Halo3GameData g : games)
		{
			varKills += Math.pow(g.getDetail().getKills() - avgKill, 2);
			varDeaths += Math.pow(g.getDetail().getDeaths() - avgDeath, 2);
			varAssists += Math.pow(g.getDetail().getAssists() - avgAssist, 2);
		}
		sdKill = Math.sqrt(varKills/games.size());
		sdDeath = Math.sqrt(varDeaths/games.size());
		sdAssist = Math.sqrt(varAssists/games.size());
	}
	
	private void resetNumbers()
	{
		kills = 0; deaths = 0; assists = 0; timePlayed = 0; wins = 0; losses = 0;
		avgKill = 0.0; avgDeath = 0.0; avgAssist = 0.0; sdKill = 0.0; sdDeath = 0.0; sdAssist = 0.0;
	}
	
	public double getAvgKill()
	{
		return avgKill;
	}
	
	public double getAvgDeath()
	{
		return avgDeath;
	}
	
	public double getAvgAssist()
	{
		return avgAssist;
	}
	
	public double getSDKill()
	{
		return sdKill;
	}
	
	public double getSDDeath()
	{
		return sdDeath;
	}
	
	public double getSDAssist()
	{
		return sdAssist;
	}
	
	public int getKills()
	{
		return kills;
	}
	
	public int getDeaths()
	{
		return deaths;
	}
	
	public int getAssists()
	{
		return assists;
	}
	
	public int getWins()
	{
		return wins;
	}
	
	public int getLosses()
	{
		return losses;
	}
	
	public int getTime()
	{
		return timePlayed;
	}
	
	public boolean isRanked()
	{
		return isRanked;
	}
	
	public String getTitle()
	{
		return title;
	}
	
	public ArrayList<Halo3GameData> getGames()
	{
		return games;
	}
	
	public void addGame(Halo3GameData g)
	{
		if(g.getInfo().getPlaylist().equals(title))
			games.add(g);
	}
	
	public int compareTo(Playlist other)
	{
		return ((Integer)(other.getGames().size())).compareTo(games.size());
	}
}
